#include "StdAfx.h"
#include "VarList.h"
#include "VarListImp.h"

#define STANDARD_GET(value) \
	_variant_t _value;\
	if(m_pVarImp->GetValue(idx, _value))\
	{\
		value = _value;\
		return true;\
	}\
\
	return false;

CVarList::CVarList(void)
{
	m_pVarImp = new CVarListImp();
}

CVarList::~CVarList(void)
{
	delete m_pVarImp;
	m_pVarImp = NULL;
}

void CVarList::VisitItems(IVarListVisitor& visitor) const
{
	m_pVarImp->VisitItems(visitor);
}

void CVarList::UpdateItems(IVarListUpdater& updater)
{
	m_pVarImp->UpdateItems(updater);
}
void CVarList::SetID(const CString& szID)
{
	m_szID = szID;
}
const CString& CVarList::ID(void)
{
	return m_szID;
}
void CVarList::SetValue(UINT idx, Standard_Real value)
{
	m_pVarImp->SetValue(idx, value);
}
bool CVarList::GetValue(UINT idx, Standard_Real& value) const
{
	STANDARD_GET(value);
	return false;
}

void CVarList::SetValue(UINT idx, Standard_Integer value)
{
	m_pVarImp->SetValue(idx, value);
}
bool CVarList::GetValue(UINT idx, Standard_Integer& value) const
{
	STANDARD_GET(value);
	return false;
}

void CVarList::SetValue(UINT idx, const CString& value)
{
	m_pVarImp->SetValue(idx, value);
}
bool CVarList::GetValue(UINT idx, CString& value) const
{
	STANDARD_GET(value);
	return false;
}
void CVarList::SetValue(UINT idx, const _variant_t& value)
{
	m_pVarImp->SetValue(idx, value);
}
bool CVarList::GetValue(UINT idx, _variant_t& value) const
{
	if(m_pVarImp->GetValue(idx, value))
		return true;

	return false;
}
void CVarList::SetValue(UINT idx, const CPointProxy& value)
{
	//CString szPoint;
	//szPoint.Format(_T("%g,%g,%g,"), value.X(), value.Y(), value.Z());
	m_pVarImp->SetValue(MAKEID_X(idx), value.X());
	m_pVarImp->SetValue(MAKEID_Y(idx), value.Y());
	m_pVarImp->SetValue(MAKEID_Z(idx), value.Z());
	//m_pVarImp->SetValue(idx, szPoint);
}
bool CVarList::GetValue(UINT idx, CPointProxy& value) const
{
	_variant_t varX, varY, varZ;

	if(!m_pVarImp->GetValue(MAKEID_X(idx), varX))
		return false;

	if(!m_pVarImp->GetValue(MAKEID_Y(idx), varY))
		return false;

	if(!m_pVarImp->GetValue(MAKEID_Z(idx), varZ))
		return false;
	//_variant_t var;
	//if(!m_pVarImp->GetValue(idx, var))
	//	return false;

	//CString szPoint = var;
	//CString szSubItem;
	//static TCHAR chStop = _T(',');

	//// x
	//int start = 0;
	//int pos = szPoint.Find(chStop, start);
	//if(pos == -1)
	//	return false;
	//szSubItem = szPoint.Mid(start, pos-start);
	//Standard_Real x = atof((LPCTSTR)szSubItem);  

	//// y
	//start = pos+1;
	//pos = szPoint.Find(chStop, start);
	//if(pos == -1)
	//	return false;
	//szSubItem = szPoint.Mid(start, pos-start);
	//Standard_Real y =  atof((LPCTSTR)szSubItem);

	//// z
	//start = pos+1;
	//pos = szPoint.Find(chStop, start);
	//if(pos == -1)
	//	return false;
	//szSubItem = szPoint.Mid(start, pos-start);
	//Standard_Real z =  atof((LPCTSTR)szSubItem);
	
	Standard_Real x, y, z;
	x = varX;
	y = varY;
	z = varZ;
	value.SetXYZ(x, y, z);

	return true;
}

//////////////////////////////////////////////////////////////////////////


CPointProxy::CPointProxy()
{
	x = y = z = 0;
}
CPointProxy::~CPointProxy()
{
}
CPointProxy::CPointProxy(const gp_Pnt& point)
{
	x = point.X();
	y = point.Y();
	z = point.Z();
}
CPointProxy::CPointProxy(const gp_Dir& point)
{
	point.Coord(x, y, z);
}
CPointProxy::operator gp_Pnt() const
{
	return gp_Pnt(x, y, z);
}
CPointProxy::operator gp_Dir() const
{
	gp_Dir dir;
	dir.SetCoord(x, y, z);

	return dir;
}

Standard_Real CPointProxy::X() const
{
	return x;
}
Standard_Real CPointProxy::Y() const
{
	return y;
}
Standard_Real CPointProxy::Z() const
{
	return z;
}
void CPointProxy::XYZ(Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
{
	X = x;
	Y = y;
	Z = z;
}
void CPointProxy::SetX(Standard_Real X)
{
	x = X;
}
void CPointProxy::SetY(Standard_Real Y)
{
	y = Y;
}
void CPointProxy::SetZ(Standard_Real Z)
{
	z = Z;
}

void CPointProxy::SetXYZ(Standard_Real X, Standard_Real Y, Standard_Real Z)
{
	x = X;
	y = Y;
	z = Z;
}